class Solution {
public:
    int hIndex(vector<int>& citations) {
        int ret = 0;
        int l = 0;
        int n = citations.size();
        int r = n-1;
        
        while(l <= r){
            int m = l + (r - l) / 2;
            int target = n - m;
            if(citations[m] >= target){
                ret = max(ret,target);
                r = m-1;
            }else{
                l = m+1;
            }
        }
        return ret;
    }
};
